﻿/******************************************************/
// TransactionPage プレゼンテーションクラス  
/******************************************************/

using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Web.UI;
using System.Web.UI.WebControls;
using MAA.Business;
using MAA.Entity;

namespace MAA.Presentation.PRL.Trading
{
    public partial class TransactionPage : System.Web.UI.Page
    {
        private ResourceManager rm = new ResourceManager("MAA.Presentation.language", System.Reflection.Assembly.GetExecutingAssembly());
        private CultureInfo m_JapanCulture = new CultureInfo("ja-JP");
        private CultureInfo m_VietnameCulture = new CultureInfo("vi");

        private void UpdateUI()
        {
            langAllPost.Text = rm.GetString("langAllPost");
            langPostProcessing.Text = rm.GetString("langPostProcessing");
            langPostSucess.Text = rm.GetString("langPostSucess");
            langPostExpiration.Text = rm.GetString("langPostExpiration");
            langAllOrder.Text = rm.GetString("langAllOrder");
            langOrderProcessing.Text = rm.GetString("langOrderProcessing");
            langOrderSucess.Text = rm.GetString("langOrderSucess");
            langOrderReject.Text = rm.GetString("langOrderReject");
            langBuy.Text = rm.GetString("langBuy");
            langRiceType.Text = rm.GetString("langRiceType");
            langProvince.Text = rm.GetString("langProvince");
            langStatus.Text = rm.GetString("langStatus");
            langSell.Text = rm.GetString("langSell");
            langRiceTypeSell.Text = rm.GetString("langRiceType");
            langProvinceSell.Text = rm.GetString("langProvince");
            langStatusSell.Text = rm.GetString("langStatus");
            dgvTransactionPost.Columns[1].HeaderText = rm.GetString("langRiceType");
            dgvTransactionPost.Columns[2].HeaderText = rm.GetString("langAmount");
            dgvTransactionPost.Columns[3].HeaderText = rm.GetString("langPriceSell");
            dgvTransactionPost.Columns[4].HeaderText = rm.GetString("langProvince");
            dgvTransactionPost.Columns[5].HeaderText = rm.GetString("langDateBuy");
            dgvTransactionPost.Columns[7].HeaderText = rm.GetString("langStatus");
            dgvTransactionOrder.Columns[1].HeaderText = rm.GetString("langRiceType");
            dgvTransactionOrder.Columns[2].HeaderText = rm.GetString("langAmount");
            dgvTransactionOrder.Columns[3].HeaderText = rm.GetString("langPriceSell");
            dgvTransactionOrder.Columns[4].HeaderText = rm.GetString("langProvince");
            dgvTransactionOrder.Columns[5].HeaderText = rm.GetString("langDateBuy");
            dgvTransactionOrder.Columns[7].HeaderText = rm.GetString("langStatus");
            imgBtnFirst_Post.ToolTip = rm.GetString("langFirst");
            imgBtnPrevious_Post.ToolTip = rm.GetString("langPrevious");
            imgBtnNext_Post.ToolTip = rm.GetString("langNext");
            imgBtnLast_Post.ToolTip = rm.GetString("langLast");
            imgBtnFirst_Order.ToolTip = rm.GetString("langFirst");
            imgBtnPrevious_Order.ToolTip = rm.GetString("langPrevious");
            imgBtnNext_Order.ToolTip = rm.GetString("langNext");
            imgBtnLast_Order.ToolTip = rm.GetString("langLast");
            ddlStatusPost.Items[0].Text = rm.GetString("langAll");
            ddlStatusPost.Items[1].Text = rm.GetString("langProcessing");
            ddlStatusPost.Items[2].Text = rm.GetString("langSucess");
            ddlStatusPost.Items[3].Text = rm.GetString("langExpiration");
            ddlStatusOrder.Items[0].Text = rm.GetString("langAll");
            ddlStatusOrder.Items[1].Text = rm.GetString("langProcessing");
            ddlStatusOrder.Items[2].Text = rm.GetString("langAccepted");
            ddlStatusOrder.Items[3].Text = rm.GetString("langReject");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["language"] == null)
            {
                Session["language"] = "vi";
            }
            if (Session["language"].Equals("vi"))
            {
                Thread.CurrentThread.CurrentUICulture = m_VietnameCulture;
                UpdateUI();
            }
            if (Session["language"].Equals("jp"))
            {
                Thread.CurrentThread.CurrentUICulture = m_JapanCulture;
                UpdateUI();
            }
            if (IsPostBack) return;
            //kiem tra da dang nhap chua
            if (Session["userID"] == null)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "popup",
                    "<script type='text/javascript'>alert('" + PRLCommon.ChangeLanguageMessage(Common.MESSAGE_ERROR_NOT_LOGIN) + "');window.location='../HomePage.aspx'</script>");
            }
            else
            {
                // Get rice type to add to drop down list
                BOLNews bolNews = new BOLNews();
                DataTable tblRiceType = new DataTable();
                string messageRiceType = "";
                string lang = Session["language"].ToString();
                if (!bolNews.viewRiceType(lang,out tblRiceType, out messageRiceType))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                       "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(messageRiceType) + "')</script>", false);
                }
                ddlRiceTypePost.DataSource = tblRiceType;
                ddlRiceTypePost.DataValueField = "ID";
                ddlRiceTypePost.DataTextField = "TypeName";
                ddlRiceTypePost.DataBind();
                if (Session["language"].Equals("vi"))
                {
                    ddlRiceTypePost.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                if (Session["language"].Equals("jp"))
                {
                    ddlRiceTypePost.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                ddlRiceTypePost.SelectedValue = "0";

                ddlRiceTypeOrder.DataSource = tblRiceType;
                ddlRiceTypeOrder.DataValueField = "ID";
                ddlRiceTypeOrder.DataTextField = "TypeName";
                ddlRiceTypeOrder.DataBind();
                if (Session["language"].Equals("vi"))
                {
                    ddlRiceTypeOrder.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                if (Session["language"].Equals("jp"))
                {
                    ddlRiceTypeOrder.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                ddlRiceTypeOrder.SelectedValue = "0";

                // Get province
                DataTable tblProvince = new DataTable();
                string messageProvince = "";
                if (!bolNews.viewProvince(lang,out tblProvince, out messageProvince))
                {
                    ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(),
                           "Alert", "<script>alert('" + PRLCommon.ChangeLanguageMessage(messageProvince) + "')</script>", false);
                }
                ddlProvincePost.DataSource = tblProvince;
                ddlProvincePost.DataValueField = "ID";
                ddlProvincePost.DataTextField = "Name";
                ddlProvincePost.DataBind();
                if (Session["language"].Equals("vi"))
                {
                    ddlProvincePost.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                if (Session["language"].Equals("jp"))
                {
                    ddlProvincePost.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                ddlProvincePost.SelectedValue = "0";

                ddlProvinceOrder.DataSource = tblProvince;
                ddlProvinceOrder.DataValueField = "ID";
                ddlProvinceOrder.DataTextField = "Name";
                ddlProvinceOrder.DataBind();
                if (Session["language"].Equals("vi"))
                {
                    ddlProvinceOrder.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                if (Session["language"].Equals("jp"))
                {
                    ddlProvinceOrder.Items.Insert(0, new ListItem(rm.GetString("langAll"), "0"));
                }
                ddlProvinceOrder.SelectedValue = "0";

                //Get post
                getPost(1);
                Session["pageIndex_Post"] = 1;
                long totalPostProcessing = getTotalPostInfo(1);
                long totalPostSuccess = getTotalPostInfo(2);
                long totalPostExpried = getTotalPostInfo(3);
                long totalPost = totalPostProcessing + totalPostSuccess + totalPostExpried;

                /////////////////////////////////////////////////////////////////////////
                lblNumPost.Text = totalPost.ToString();
                lblNumPostTrading.Text = totalPostProcessing.ToString();
                lblNumPostSuccess.Text = totalPostSuccess.ToString();
                lblNumPostExpired.Text = totalPostExpried.ToString();

                //Get order
                getOrder(1);
                Session["pageIndex_Order"] = 1;
                long totalOrderProcessing = getTotalOrderInfo(1);
                long totalOrderAccepted = getTotalOrderInfo(2);
                long totalOrderRejected = getTotalOrderInfo(3);
                long totalOrder = totalOrderProcessing + totalOrderAccepted + totalOrderRejected;

                /////////////////////////////////////////////////////////////////////////
                lblNumOrder.Text = totalOrder.ToString();
                lblNumOrderPending.Text = totalOrderProcessing.ToString();
                lblNumOrderAccept.Text = totalOrderAccepted.ToString();
                lblNumOrderReject.Text = totalOrderRejected.ToString();
            }
        }

        protected void getPost(int pageIndex)
        {
            //get userID
            long userID = Convert.ToInt64(Session["userID"]);
            int riceID = Convert.ToInt32(ddlRiceTypePost.SelectedItem.Value);
            short provinceID = Convert.ToInt16(ddlProvincePost.SelectedItem.Value);
            short statusID = Convert.ToInt16(ddlStatusPost.SelectedItem.Value);
            BOLTrading bolTrading = new BOLTrading();
            DataTable tblTransactionPost = new DataTable();
            string message = "";
            long rowNum = 0;

            int pageSize = dgvTransactionPost.PageSize;

            //Get Transaction
            lblPostsMess.Text = null;
            string lang = Session["language"].ToString();
            if (!bolTrading.viewTransactionPost(lang,pageIndex, pageSize, userID, riceID, provinceID, statusID, out tblTransactionPost, out rowNum, out message))
            {
                lblPostsMess.Text = PRLCommon.ChangeLanguageMessage(message);
            }
            else
            {
                dgvTransactionPost.DataSource = tblTransactionPost;
                dgvTransactionPost.DataBind();
                // Neu co du lieu
                if (dgvTransactionPost.Rows.Count > 0)
                {
                    ImageMap imNotiPost;
                    for (int i = 0; i < dgvTransactionPost.Rows.Count; i++)
                    {
                        imNotiPost = (ImageMap)dgvTransactionPost.Rows[i].FindControl("imNotiPost");
                        if (Convert.ToInt16(tblTransactionPost.Rows[i]["Noti"]) == 1)
                        {
                            dgvTransactionPost.Columns[8].Visible = true;
                            imNotiPost.Visible = true;
                        }
                    }
                    if (rowNum > dgvTransactionPost.PageSize)
                    {
                        Pager_Post(rowNum, pageIndex);
                        enablePaging_Post(true);
                    }
                    else enablePaging_Post(false);
                }
                else
                {
                    enablePaging_Post(false);
                    lblPostsMess.Text = PRLCommon.ChangeLanguageMessage(Common.MESSAGE_NO_DATA_POST);
                }
            }
        }

        protected void getOrder(int pageIndex)
        {
            //get userID
            long userID = Convert.ToInt64(Session["userID"]);
            int riceID = Convert.ToInt32(ddlRiceTypeOrder.SelectedItem.Value);
            short provinceID = Convert.ToInt16(ddlProvinceOrder.SelectedItem.Value);
            short statusID = Convert.ToInt16(ddlStatusOrder.SelectedItem.Value);
            BOLTrading bolTrading = new BOLTrading();
            DataTable tblTransactionOrder = new DataTable();
            string message = "";
            long rowNum = 0;

            int pageSize = dgvTransactionOrder.PageSize;

            //Get Transaction
            lblOrderMess.Text = null;
            string lang = Session["language"].ToString();
            if (!bolTrading.viewTransactionOrder(lang,pageIndex, pageSize, userID, riceID, provinceID, statusID, out tblTransactionOrder, out rowNum, out message))
            {
                lblOrderMess.Text = PRLCommon.ChangeLanguageMessage(message);
            }
            else
            {
                dgvTransactionOrder.DataSource = tblTransactionOrder;
                dgvTransactionOrder.DataBind();
                // Neu co du lieu
                if (dgvTransactionOrder.Rows.Count > 0)
                {
                    if (rowNum > dgvTransactionOrder.PageSize)
                    {
                        Pager_Order(rowNum, pageIndex);
                        enablePaging_Order(true);
                    }
                    else enablePaging_Order(false);
                }
                else
                {
                    enablePaging_Order(false);
                    lblOrderMess.Text = PRLCommon.ChangeLanguageMessage(Common.MESSAGE_NO_DATA_ORDER);
                }
            }
        }

        // Change number of page Post
        private void Pager_Post(long recordCount, int currentPage)
        {
            lblCurrentPage_Post.Text = currentPage.ToString();

            double dblPageCount = (double)((decimal)recordCount / decimal.Parse(dgvTransactionPost.PageSize.ToString()));
            int pageCount = (int)Math.Ceiling(dblPageCount);
            lblTotalPage_Post.Text = pageCount.ToString();
        }

        // Change number of page Order
        private void Pager_Order(long recordCount, int currentPage)
        {
            lblCurrentPage_Order.Text = currentPage.ToString();

            double dblPageCount = (double)((decimal)recordCount / decimal.Parse(dgvTransactionOrder.PageSize.ToString()));
            int pageCount = (int)Math.Ceiling(dblPageCount);
            lblTotalPage_Order.Text = pageCount.ToString();
        }

        // Xu ly khi nguoi dung chuyen trang
        protected void Page_Changed_Post(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(Session["pageIndex_Post"].ToString());
            int pageCount = Convert.ToInt32(lblTotalPage_Post.Text);

            if ((sender as ImageButton).CommandArgument == "First")
            {
                if (pageIndex == 1) return;
                pageIndex = 1;
            }

            if ((sender as ImageButton).CommandArgument == "Previous")
            {
                pageIndex = pageIndex - 1;
                if (pageIndex < 1) return;
            }

            if ((sender as ImageButton).CommandArgument == "Next")
            {
                pageIndex = pageIndex + 1;
                if (pageIndex > pageCount) return;
            }

            if ((sender as ImageButton).CommandArgument == "Last")
            {
                if (pageIndex == pageCount) return;
                pageIndex = pageCount;
            }
            Session["pageIndex_Post"] = pageIndex;
            getPost(pageIndex);
        }

        // Xu ly khi nguoi dung chuyen trang
        protected void Page_Changed_Order(object sender, EventArgs e)
        {
            int pageIndex = int.Parse(Session["pageIndex_Order"].ToString());
            int pageCount = Convert.ToInt32(lblTotalPage_Order.Text);

            if ((sender as ImageButton).CommandArgument == "First")
            {
                if (pageIndex == 1) return;
                pageIndex = 1;
            }

            if ((sender as ImageButton).CommandArgument == "Previous")
            {
                pageIndex = pageIndex - 1;
                if (pageIndex < 1) return;
            }

            if ((sender as ImageButton).CommandArgument == "Next")
            {
                pageIndex = pageIndex + 1;
                if (pageIndex > pageCount) return;
            }

            if ((sender as ImageButton).CommandArgument == "Last")
            {
                if (pageIndex == pageCount) return;
                pageIndex = pageCount;
            }
            Session["pageIndex_Order"] = pageIndex;
            getOrder(pageIndex);
        }

        protected void SelectedIndexChanged_Post(object sender, EventArgs e)
        {
            getPost(1);
        }

        protected void SelectedIndexChanged_Order(object sender, EventArgs e)
        {
            getOrder(1);
        }

        protected long getTotalPostInfo(short statusID)
        {
            //get userID
            long userID = Convert.ToInt64(Session["userID"]);
            int riceID = 0;
            short provinceID = 0;
            BOLTrading bolTrading = new BOLTrading();
            DataTable tblTransactionPost = new DataTable();
            string message = "";
            int pageSize = 1;

            long rowNum = 0;
            //Get Transaction
            string lang = Session["language"].ToString();
            if (!bolTrading.viewTransactionPost(lang,1, pageSize, userID, riceID, provinceID, statusID, out tblTransactionPost, out rowNum, out message))
            {
                lblPostsMess.Text = PRLCommon.ChangeLanguageMessage(message);
            }
            return rowNum;
        }

        protected long getTotalOrderInfo(short statusID)
        {
            //get userID
            long userID = Convert.ToInt64(Session["userID"]);
            int riceID = 0;
            short provinceID = 0;
            BOLTrading bolTrading = new BOLTrading();
            DataTable tblTransactionOrder = new DataTable();
            string message = "";
            int pageSize = 1;

            long rowNum = 0;
            string lang = Session["language"].ToString();
            //Get Transaction
            if (!bolTrading.viewTransactionOrder(lang,1, pageSize, userID, riceID, provinceID, statusID, out tblTransactionOrder, out rowNum, out message))
            {
                lblOrderMess.Text = PRLCommon.ChangeLanguageMessage(message);
            }
            return rowNum;
        }

        private void enablePaging_Post(bool enable)
        {
            if (enable == true)
            {
                imgBtnFirst_Post.Visible = true;
                imgBtnPrevious_Post.Visible = true;
                lblCurrentPage_Post.Visible = true;
                lblSeperate_Post.Visible = true;
                lblTotalPage_Post.Visible = true;
                imgBtnNext_Post.Visible = true;
                imgBtnLast_Post.Visible = true;
            }
            else
            {
                imgBtnFirst_Post.Visible = false;
                imgBtnPrevious_Post.Visible = false;
                lblCurrentPage_Post.Visible = false;
                lblSeperate_Post.Visible = false;
                lblTotalPage_Post.Visible = false;
                imgBtnNext_Post.Visible = false;
                imgBtnLast_Post.Visible = false;
            }
        }

        private void enablePaging_Order(bool enable)
        {
            if (enable == true)
            {
                imgBtnFirst_Order.Visible = true;
                imgBtnPrevious_Order.Visible = true;
                lblCurrentPage_Order.Visible = true;
                lblSeperate_Order.Visible = true;
                lblTotalPage_Order.Visible = true;
                imgBtnNext_Order.Visible = true;
                imgBtnLast_Order.Visible = true;
            }
            else
            {
                imgBtnFirst_Order.Visible = false;
                imgBtnPrevious_Order.Visible = false;
                lblCurrentPage_Order.Visible = false;
                lblSeperate_Order.Visible = false;
                lblTotalPage_Order.Visible = false;
                imgBtnNext_Order.Visible = false;
                imgBtnLast_Order.Visible = false;
            }
        }

        protected void dgvTransactionPost_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageMap imNoti = (ImageMap)e.Row.FindControl("imNotiPost");
                imNoti.ToolTip = rm.GetString("langNoti");
                e.Row.Cells[7].Text = PRLCommon.ChangeLanguageStatus(e.Row.Cells[7].Text);
            }
        }

        protected void dgvTransactionOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[7].Text = PRLCommon.ChangeLanguageStatus(e.Row.Cells[7].Text);
            }
        }
    }
}